package com.xcs.wx.util;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import okhttp3.*;
import java.io.IOException;
import java.util.concurrent.TimeUnit;


public class DashScopeClientUtil {
    private static final String API_URL = "https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation";

    public static void main(String[] args) {
        // 从环境变量获取 API Key
        String apiKey =  "sk-47c3d447f4bd4fc388cd2e118738b75c";
//        if (apiKey == null || apiKey.trim().isEmpty()) {
//            System.err.println("错误：请设置环境变量 DASHSCOPE_API_KEY");
//            return;
//        }

        JSONObject jsonObject = new JSONObject();
        JSONObject messageObject = new JSONObject();

        jsonObject.put("model", "deepseek-v3.1");

        JSONObject jsonSystem = new JSONObject();
        JSONObject jsonUser = new JSONObject();

        JSONArray jsonArray = new JSONArray();
        jsonSystem.put("role", "system");
        jsonSystem.put("content", "你是一名公安情报分析AI助手，负责对监控获取的微信群聊记录进行安全威胁评估。请根据以下300条原始聊天上下文，逐条分析并汇总输出高价值情报信息。\n" +
                "\n" +
                "【输入数据格式】\n" +
                "每条数据是json格式：msgSvrId是消息服务器ID，strContent是消息内容，strCreateTime是消息创建时间，wxid是微信id,talkId是群聊id\n" +
                "示例：{\"msgSvrId\":\"5939195414025940856\",\"strContent\":\"购买VIP会员可以享有免费同城约炮、观看海量的色情视频和性爱直播等资源\",\"strCreateTime\":\"1970-01-12 21:49:32\",\"wxId\":\"wxid_u4u8wxz8evkg22\",\"talkerId\":\"52244429837@chatroom\"}\n" +
                "\n" +
                "【任务要求】\n" +
                "请完成以下分析任务，并以标准JSON格式返回结果：\n" +
                "\n" +
                "1. **原文提取**：识别出涉及“约炮”、“色情”等敏感行为的原始句子。\n" +
                "2. **关键词提取**：从原句中提取关键敏感词（如“约炮”、“色情”、“刷单”、“返利”、“赌球”、“庄家”等）。\n" +
                "3. **关键信息抽取**：\n" +
                "   - 微信id\n" +
                "   - 时间\n" +
                "   - 涉及事件类型（约炮 / 色情 / 涉赌）\n" +
                "   - 具体行为描述（如“约炮刷单”、“参与网络刷单诈骗”、“组织赌球”）\n" +
                "4. **事件总结与描述**：\n" +
                "   - 用一句话概括该条信息的核心事件\n" +
                "   - 描述其潜在危害性与紧迫性\n" +
                "5. **AI研判逻辑**：\n" +
                "   - 分析依据：基于哪些关键词、语义、上下文判断为该事件\n" +
                "   - 情绪判断：如“极端负面”、“诱导性强”、“组织化倾向”\n" +
                "   - 行为阶段：如“仅提及”、“已有计划”、“正在实施”\n" +
                "6. **威胁等级判定**：\n" +
                "   - 高：有明确时间、地点、行为准备，或涉及多人、未成年人\n" +
                "   - 中：有倾向但无具体计划，或情绪表达强烈但无行动迹象\n" +
                "   - 低：仅泛泛提及，无具体指向\n" +
                "\n" +
                "【输出格式】\n" +
                "请返回一个标准格式的JSON数组，每条记录结构如下：\n" +
                "{\n" +
                "  \"strContent\": \"原始句子\",\n" +
                "  \"keywords\": [\"关键词1\", \"关键词2\"],\n" +
                "  \"wxId\": \"微信id\",\n" +
                "  \"talkId\":\"群聊id\",\n" +
                "  \"msgSvrId\": \"消息id\",\n" +
                "  \"timestamp\": \"YYYY-MM-DD HH:MM:SS\",\n" +
                "  \"event_type\": \"约炮 | 色情 | 涉赌\",\n" +
                "  \"behavior\": \"具体行为描述\",\n" +
                "  \"summary\": \"事件一句话总结\",\n" +
                "  \"description\": \"事件详细描述及潜在风险\",\n" +
                "  \"analysis_logic\": {\n" +
                "    \"evidence\": [\"判断依据关键词或语义\"],\n" +
                "    \"emotion\": \"情绪状态\",\n" +
                "    \"stage\": \"行为阶段\"\n" +
                "  },\n" +
                "  \"threat_level\": \"高 | 中 | 低\",\n" +
                "  \"confidence_score\": 0.00  // 判断置信度，保留两位小数\n" +
                "}\n" +
                "\n" +
                "【处理范围】\n" +
                "请分析Json中每一条的聊天记录，仅返回判定为“中”及以上威胁等级的条目，按时间倒序排列。\n" +
                "\n" +
                "【特别说明】\n" +
                "- 保持客观严谨，避免误判，但宁可错报不可漏报\n" +
                "- 对隐喻、暗语、谐音词（如“走”代指自杀，“杀猪盘”代指诈骗）需特别识别\n" +
                "- 多条消息关联时，可合并为一条事件进行研判\n" +
                "- 返回json数据时校验一下json格式是否正确,如果json中的description涉及到敏感词，用‘’进行显示");

        jsonUser.put("role", "user");
        jsonUser.put("content", "现在开始处理以下聊天记录：\n" + "[{\"msgSvrId\":\"3861630029558305091\",\"strContent\":\"呜呜呜呜呜呜呜\",\"strCreateTime\":\"2025-08-28 15:03:27\",\"wxId\":\"wxid_u4u8wxz8evkg22\"},{\"msgSvrId\":\"3400450233486330505\",\"strContent\":\"fdsjklghfjisghbndf\",\"strCreateTime\":\"2025-08-28 15:03:40\",\"wxId\":\"wxid_f958x26hyqkq22\"},{\"msgSvrId\":\"6458067273007314538\",\"strContent\":\"56456465\",\"strCreateTime\":\"2025-08-28 15:03:41\",\"wxId\":\"wxid_f958x26hyqkq22\"},{\"msgSvrId\":\"7667092795489219397\",\"strContent\":\"杀了你\",\"strCreateTime\":\"2025-08-28 15:03:45\",\"wxId\":\"wxid_f958x26hyqkq22\"},{\"msgSvrId\":\"1990989261165186444\",\"strContent\":\"34\",\"strCreateTime\":\"2025-08-28 15:03:50\",\"wxId\":\"wxid_ldlxft9vp7jx12\"},{\"msgSvrId\":\"7142595754566444240\",\"strContent\":\"4242\",\"strCreateTime\":\"2025-08-28 15:03:50\",\"wxId\":\"wxid_ldlxft9vp7jx12\"},{\"msgSvrId\":\"3977293905788258466\",\"strContent\":\"234\",\"strCreateTime\":\"2025-08-28 15:03:51\",\"wxId\":\"wxid_ldlxft9vp7jx12\"},{\"msgSvrId\":\"3069272280006181575\",\"strContent\":\"234\",\"strCreateTime\":\"2025-08-28 15:03:51\",\"wxId\":\"wxid_ldlxft9vp7jx12\"},{\"msgSvrId\":\"6386123257236669620\",\"strContent\":\"324\",\"strCreateTime\":\"2025-08-28 15:03:51\",\"wxId\":\"wxid_ldlxft9vp7jx12\"},{\"msgSvrId\":\"4585062410351905591\",\"strContent\":\"234\",\"strCreateTime\":\"2025-08-28 15:03:51\",\"wxId\":\"wxid_ldlxft9vp7jx12\"},{\"msgSvrId\":\"3956870407931444918\",\"strContent\":\"dffsdf'sdfsdf的\",\"strCreateTime\":\"2025-08-28 15:03:53\",\"wxId\":\"wxid_ldlxft9vp7jx12\"},{\"msgSvrId\":\"6228311295975212564\",\"strContent\":\"方式鬼斧神工萨公司\",\"strCreateTime\":\"2025-08-28 15:03:54\",\"wxId\":\"wxid_ldlxft9vp7jx12\"},{\"msgSvrId\":\"4748448817121146543\",\"strContent\":\"发对方水电费胜多负少\",\"strCreateTime\":\"2025-08-28 15:03:56\",\"wxId\":\"wxid_ldlxft9vp7jx12\"},{\"msgSvrId\":\"3157119358959781593\",\"strContent\":\"沙发沙发萨芬萨芬\",\"strCreateTime\":\"2025-08-28 15:03:57\",\"wxId\":\"wxid_ldlxft9vp7jx12\"},{\"msgSvrId\":\"2507905668748175296\",\"strContent\":\"发放_(:з」∠)_三个月前太过分分隔符是撒\",\"strCreateTime\":\"2025-08-28 15:03:59\",\"wxId\":\"wxid_ldlxft9vp7jx12\"},{\"msgSvrId\":\"8405458052763253822\",\"strContent\":\"东莞萨公司讽德诵功懂法守法\",\"strCreateTime\":\"2025-08-28 15:04:01\",\"wxId\":\"wxid_ldlxft9vp7jx12\"},{\"msgSvrId\":\"2518711206081052368\",\"strContent\":\"1111\",\"strCreateTime\":\"2025-08-28 15:07:06\",\"wxId\":\"wxid_u4u8wxz8evkg22\"},{\"msgSvrId\":\"699363581923210934\",\"strContent\":\"11\",\"strCreateTime\":\"2025-08-28 15:07:07\",\"wxId\":\"wxid_u4u8wxz8evkg22\"},{\"msgSvrId\":\"4436328339264442467\",\"strContent\":\"余额宝收益\",\"strCreateTime\":\"2025-08-28 15:16:00\",\"wxId\":\"wxid_u4u8wxz8evkg22\"},{\"msgSvrId\":\"8247592824310600815\",\"strContent\":\"嘎嘎嘎\",\"strCreateTime\":\"2025-08-28 15:17:35\",\"wxId\":\"wxid_u4u8wxz8evkg22\"},{\"msgSvrId\":\"8446171203176919218\",\"strContent\":\"嘎嘎嘎\",\"strCreateTime\":\"2025-08-28 15:22:47\",\"wxId\":\"wxid_f958x26hyqkq22\"},{\"msgSvrId\":\"2240273921500668159\",\"strContent\":\"111\",\"strCreateTime\":\"2025-08-28 15:37:16\",\"wxId\":\"wxid_u4u8wxz8evkg22\"},{\"msgSvrId\":\"4765686648711238146\",\"strContent\":\"1111111\",\"strCreateTime\":\"2025-08-29 10:31:40\",\"wxId\":\"wxid_u4u8wxz8evkg22\"},{\"msgSvrId\":\"8730800320243156711\",\"strContent\":\"456456456456\",\"strCreateTime\":\"2025-08-29 10:31:54\",\"wxId\":\"wxid_f958x26hyqkq22\"},{\"msgSvrId\":\"9015214088470580477\",\"strContent\":\"为啥接收不到信息\",\"strCreateTime\":\"2025-08-29 10:58:50\",\"wxId\":\"wxid_u4u8wxz8evkg22\"},{\"msgSvrId\":\"1156802567556911600\",\"strContent\":\"因为是七夕\",\"strCreateTime\":\"2025-08-29 10:59:26\",\"wxId\":\"wxid_ldlxft9vp7jx12\"},{\"msgSvrId\":\"5580258204480388003\",\"strContent\":\"11111111\",\"strCreateTime\":\"2025-08-29 15:29:55\",\"wxId\":\"wxid_u4u8wxz8evkg22\"},{\"msgSvrId\":\"4065618550307037136\",\"strContent\":\"2222222\",\"strCreateTime\":\"2025-08-29 15:29:58\",\"wxId\":\"wxid_u4u8wxz8evkg22\"},{\"msgSvrId\":\"6554613508445433310\",\"strContent\":\"\uD83D\uDE06【财富+“薪”计划】代发薪享\\n\uD83D\uDC95来民生代发，心动不止一下\\n\uD83C\uDF08甄选产品，薪享配置\\n\uD83D\uDCB0代发专属，薪上加薪\\n\uD83C\uDF81回复“1”解锁定制“理”物~\",\"strCreateTime\":\"2025-08-29 15:31:19\",\"wxId\":\"wxid_u4u8wxz8evkg22\"},{\"msgSvrId\":\"5409478712067579552\",\"strContent\":\"1\",\"strCreateTime\":\"2025-08-29 15:31:50\",\"wxId\":\"wxid_f958x26hyqkq22\"},{\"msgSvrId\":\"360990322014175641\",\"strContent\":\"裂开\",\"strCreateTime\":\"2025-08-29 15:50:14\",\"wxId\":\"wxid_u4u8wxz8evkg22\"},{\"msgSvrId\":\"8289112568397394953\",\"strContent\":\"诈骗\",\"strCreateTime\":\"2025-09-08 16:28:43\",\"wxId\":\"wxid_u4u8wxz8evkg22\"},{\"msgSvrId\":\"2973147724257618736\",\"strContent\":\"我要赌博100w\",\"strCreateTime\":\"2025-09-08 16:29:11\",\"wxId\":\"wxid_u4u8wxz8evkg22\"},{\"msgSvrId\":\"2758024410352484133\",\"strContent\":\"啊啊啊啊\",\"strCreateTime\":\"2025-09-08 16:29:16\",\"wxId\":\"wxid_u4u8wxz8evkg22\"},{\"msgSvrId\":\"8487235450572919507\",\"strContent\":\"aaaa\",\"strCreateTime\":\"2025-09-08 16:29:17\",\"wxId\":\"wxid_u4u8wxz8evkg22\"},{\"msgSvrId\":\"5565825101329578097\",\"strContent\":\"我要吃屎\",\"strCreateTime\":\"2025-09-08 16:29:18\",\"wxId\":\"wxid_f958x26hyqkq22\"},{\"msgSvrId\":\"5939195414025940856\",\"strContent\":\"购买VIP会员可以享有免费同城约炮、观看海量的色情视频和性爱直播等资源\",\"strCreateTime\":\"2025-09-12 09:49:57\",\"wxId\":\"wxid_u4u8wxz8evkg22\"},{\"msgSvrId\":\"4915100576145599614\",\"strContent\":\"哥哥，如果你购买 VIP 会员就可以拥有免费同城啪啪的资源，还可以无限观看萝莉、学生、嫩模、少妇的激情啪啪的视频和直播呦\",\"strCreateTime\":\"2025-09-12 09:50:18\",\"wxId\":\"wxid_u4u8wxz8evkg22\"},{\"msgSvrId\":\"5690797549170954251\",\"strContent\":\"[色]\",\"strCreateTime\":\"2025-09-12 09:50:33\",\"wxId\":\"wxid_f958x26hyqkq22\"},{\"msgSvrId\":\"7977981970446000952\",\"strContent\":\"真的假的\",\"strCreateTime\":\"2025-09-12 09:50:38\",\"wxId\":\"wxid_f958x26hyqkq22\"},{\"msgSvrId\":\"2582109358027008421\",\"strContent\":\"怎么购买\",\"strCreateTime\":\"2025-09-12 09:50:49\",\"wxId\":\"wxid_f958x26hyqkq22\"},{\"msgSvrId\":\"3224161138591934955\",\"strContent\":\"哥哥，扫这个二维码支付 588元就可以成为 VIP 会员了呢\",\"strCreateTime\":\"2025-09-12 09:50:53\",\"wxId\":\"wxid_u4u8wxz8evkg22\"},{\"msgSvrId\":\"4577104338633273218\",\"strContent\":\"哥哥，新会员需要进行数据认证才能开卡哒\",\"strCreateTime\":\"2025-09-12 09:51:08\",\"wxId\":\"wxid_u4u8wxz8evkg22\"},{\"msgSvrId\":\"1532762332869385439\",\"strContent\":\"先给哥哥说下，这个约炮资源和看片资源是公司免费提供的，平台需要提高真实的投票认证量。需要完成3次数据认证来开通你得 VIP会员预约卡，完成后即可永远看片看直播和进同城微信群，安排美眉到你预订的酒店(新人免费赠送订房1次)\",\"strCreateTime\":\"2025-09-12 09:51:22\",\"wxId\":\"wxid_u4u8wxz8evkg22\"},{\"msgSvrId\":\"8508866862907173802\",\"strContent\":\"哥哥，新会员只有完成首次约炮认证过后，下次直接在平台约美眉即可，不需要再次认证啦。\",\"strCreateTime\":\"2025-09-12 09:51:35\",\"wxId\":\"wxid_u4u8wxz8evkg22\"},{\"msgSvrId\":\"4287185057156349500\",\"strContent\":\"哥哥，麻烦你提供一下你的银行卡账号，这个钱会分文不少的返还给你，还额外加 20%的佣金\",\"strCreateTime\":\"2025-09-12 09:52:03\",\"wxId\":\"wxid_u4u8wxz8evkg22\"}]\n");
        jsonArray.add(jsonSystem);
        jsonArray.add(jsonUser);
        messageObject.put("messages", jsonArray);
        jsonObject.put("input",messageObject);
        JSONObject object = new JSONObject();
//        object.put("include_usage",true);
//        jsonObject.put("parameters",object);
//
//        jsonObject.put("stream",true);
//        jsonObject.put("enable_thinking",true);
        String jsonString = jsonObject.toJSONString();

        // 创建 OkHttpClient 实例（可复用）
        OkHttpClient client = new OkHttpClient().newBuilder()
                .connectTimeout(10, TimeUnit.SECONDS)   // 连接超时
                .readTimeout(60, TimeUnit.SECONDS)      // 👈 读取超时设为 60 秒！
                .writeTimeout(10, TimeUnit.SECONDS)
                .retryOnConnectionFailure(true)
                .build();
        // 构造 Request
        Request request = new Request.Builder()
                .url(API_URL)
                .post(RequestBody.create(
                        MediaType.parse("application/json; charset=utf-8"),
                        jsonString
                                ))
                .addHeader("Authorization", "Bearer " + apiKey)
                .addHeader("Content-Type", "application/json")
                .build();

        // 发送异步请求（推荐）或使用 .execute() 同步请求
        try (Response response = client.newCall(request).execute()) {
            if (!response.isSuccessful()) {
                System.err.println("请求失败，状态码：" + response.code());
            }

            String responseBody = response.body() != null ? response.body().string() : "";
            System.out.println("状态码: " + response.code());
            System.out.println("响应内容: " + responseBody);

        } catch (IOException e) {
            System.err.println("网络请求异常: " + e.getMessage());
            e.printStackTrace();
        }
    }
}
